﻿using System;
using System.Collections.Generic;
using System.Text;

public class SeqStack<T> : IStack<T>
{
    private int maxsize; // 顺序栈的容量
    private T[] data; // 数组，用于存储顺序栈中元素的
    private int top;// 指示顺序栈的栈顶

    public T this[int index]
    {
        get
        {
            return data[index];
        }
        set
        {
            data[index] = value;
        }
    }

    public int Maxsize { get { return maxsize; } set { maxsize = value; } }
    public int Top { get { return top; } }

    public SeqStack(int size)
    {
        data = new T[size];
        maxsize = size;
        top = -1;
    }

    public void Clear()
    {
        top = -1;
    }

    public int GetLength()
    {
        return top + 1;
    }

    public T GetTop()
    {
        if (IsEmpty())
        {
            Console.WriteLine("Stack is empty!");
            return default(T);
        }
        return data[top];
    }

    public bool IsEmpty()
    {
        if (top == -1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsFull()
    {
        if (top == maxsize - 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public T Pop()
    {
        T tmp = default(T);
        if (IsEmpty())
        {
            Console.WriteLine("Stack is empty");
            return tmp;
        }

        tmp = data[top];
        --top;
        return tmp;
    }

    public void Push(T item)
    {
        if (IsFull())
        {
            Console.WriteLine("Stack is full");
            return;
        }
        data[++top] = item;
    }
}
